package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.FillingWareReg;
import com.gljx.web.Entity.FillingWareRegEntry;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.FillingWareRegEntryMapper;
import com.gljx.web.dao.FillingWareRegMapper;
import com.gljx.web.service.FillingWareRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FillingWareRegServiceImpl extends CommonDao implements FillingWareRegService {

	@Autowired
	private FillingWareRegMapper fillingWareRegMapper;
	
	@Autowired
	private FillingWareRegEntryMapper fillingWareRegEntryMapper;
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.FillingWareRegMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.FillingWareRegMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return fillingWareRegMapper.selectFnumber(companyId);
	}

	@Override
	public int addFillingWareReg(FillingWareReg fillingWareReg, String[] fseq, String[] oilId, String[] cfqty,
			String[] cfprice, String[] cfamount, String[] cfdesc) {
		// TODO Auto-generated method stub
		int row1=0;
		int row2=0;
		//添加父类
		row1=fillingWareRegMapper.insertSelective(fillingWareReg);
		//添加子类
		for (int i = 0; i < fseq.length; i++) {
			FillingWareRegEntry fillingWareRegEntry = new FillingWareRegEntry();
			fillingWareRegEntry.setFid(GetFid.UUIDIntoFid());
			fillingWareRegEntry.setFseq(Long.valueOf(fseq[i]));
			fillingWareRegEntry.setFparentid(fillingWareReg.getFid());
			fillingWareRegEntry.setCfoilspecid(oilId[i]);
			fillingWareRegEntry.setCfnums(new BigDecimal(cfqty[i]));	
			fillingWareRegEntry.setCfprice(new BigDecimal(cfprice[i]));
			fillingWareRegEntry.setCfamount(new BigDecimal(cfamount[i]));
			fillingWareRegEntry.setCfremark(cfdesc[i]);
			row2+=fillingWareRegEntryMapper.insertSelective(fillingWareRegEntry);
		}
		if(row1>0 && row2>0){
			return 1;
		}
		return 0;
	}

	@Override
	public int updateFillingWareReg(FillingWareReg fillingWareReg) {
		// TODO Auto-generated method stub
		return fillingWareRegMapper.updateByPrimaryKeySelective(fillingWareReg);
	}

	@Override
	public int updateFillingWareReg(FillingWareReg fillingWareReg, String[] fseq, String[] oilId, String[] cfqty,
			String[] cfprice, String[] cfamount, String[] cfdesc) {
		// TODO Auto-generated method stub
		//先删除子类
		fillingWareRegEntryMapper.deleteByParentId(fillingWareReg.getFid());
		//修改父类
		 int row1 = fillingWareRegMapper.updateByPrimaryKeySelective(fillingWareReg);
		//添加子类
		int row2=0;
		for (int i = 0; i < fseq.length; i++) {
			FillingWareRegEntry fillingWareRegEntry = new FillingWareRegEntry();
			fillingWareRegEntry.setFid(GetFid.UUIDIntoFid());
			fillingWareRegEntry.setFseq(Long.valueOf(fseq[i]));
			fillingWareRegEntry.setFparentid(fillingWareReg.getFid());
			fillingWareRegEntry.setCfoilspecid(oilId[i]);
			fillingWareRegEntry.setCfnums(new BigDecimal(cfqty[i]));	
			fillingWareRegEntry.setCfprice(new BigDecimal(cfprice[i]));
			fillingWareRegEntry.setCfamount(new BigDecimal(cfamount[i]));
			fillingWareRegEntry.setCfremark(cfdesc[i]);
			row2+=fillingWareRegEntryMapper.insertSelective(fillingWareRegEntry);
		}
		if(row1 > 0 && row2 >0){
			return  1;
		}
		return 0;
	}

	@Override
	public int deleteFillingWareRegByFid(String fid) {
		// TODO Auto-generated method stub
		return fillingWareRegMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public FillingWareReg selFillingWareRegByFid(String fid) {
		// TODO Auto-generated method stub
		return fillingWareRegMapper.selectByPrimaryKey(fid);
	}

	@Override
	public List<FillingWareReg> selectFillingWareReg(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return fillingWareRegMapper.selectFillingWareReg(map);
	}

}
